package com.originskyseed.platform.count.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.count.mapper.UserRechargeMapper;
import com.originskyseed.platform.count.model.UserRecharge;
import com.originskyseed.platform.count.service.IUserRechargeService;

/**
 * 用户充值信息表Service接口实现类（OSI_USER_RECHARGE）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("userRechargeService")
public class UserRechargeServiceImpl implements IUserRechargeService {

	@Resource
	private UserRechargeMapper userRechargeMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.userRechargeMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.userRechargeMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(UserRecharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userRechargeMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(UserRecharge record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userRechargeMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(UserRecharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userRechargeMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(UserRecharge record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userRechargeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserRecharge selectByPrimaryKey(String id) throws Exception {
		return this.userRechargeMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserRecharge> selectList(UserRecharge record) throws Exception {
		return this.userRechargeMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserRecharge> selectListByPage(Map<String, Object> params, BasePage<UserRecharge> page) throws Exception {
		params.put("page", page);
		List<UserRecharge> list = this.userRechargeMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量插入
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertBatch(List<UserRecharge> list) throws Exception {
		return this.userRechargeMapper.insertBatch(list);
	}

	/**
	 * 根据日期统计业务员数据
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public List<UserRecharge> selectListByTime(String date) throws Exception {
		return this.userRechargeMapper.selectListByTime(date);
	}

	/**
	 * 根据日期新增业务员每日充值数据
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public int insertsUserRechargeByTime(String date, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		List<UserRecharge> userRechargeList = this.userRechargeMapper.selectListByTime(date);
		for (UserRecharge userRecharge : userRechargeList) {
			userRecharge.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			userRecharge.setDate(date);
			userRecharge.setCreateTime(new Date());
		}

		int count = 0;
		// 一次性插入的数量
		int size = 500;
		// 总长度
		int length = userRechargeList.size();
		// 计算可以分成多少组
		int num = (length + size - 1) / size;
		for (int i = 0; i < num; i++) {
			// 开始位置
			int fromIndex = i * size;
			// 结束位置
			int toIndex = (i + 1) * size < length ? (i + 1) * size : length;
			List<UserRecharge> newUserRechargeList = userRechargeList.subList(fromIndex, toIndex);
			count += userRechargeMapper.insertBatch(newUserRechargeList);
		}
		return count;
	}
}